import { createRouter, createWebHistory } from 'vue-router'
import MainConverter from '@/components/MainConverter.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: MainConverter,
      meta: {
        title: 'MCP JSON2TOML - JSON到TOML转换工具',
        description: '将MCP配置从JSON格式转换为TOML格式的在线工具，支持Windows环境变量自动添加',
      },
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  },
})

// 路由守卫：设置页面标题和描述
router.beforeEach((to, from, next) => {
  if (to.meta?.title) {
    document.title = to.meta.title as string
  }

  if (to.meta?.description) {
    const metaDescription = document.querySelector('meta[name="description"]')
    if (metaDescription) {
      metaDescription.setAttribute('content', to.meta.description as string)
    }
  }

  next()
})

export default router
